package controllers;

import java.lang.reflect.Constructor;
import java.util.List;

import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;
import util.Util;
import controllers.CRUD.ObjectType;
import models.Manager;
import models.Permis;
import models.PermisAmong;
import models.Role;

@With(Secure.class)
@CRUD.For(Manager.class)
public class Managers extends CRUD {
	
	
	@Check("ServiceCenter")
	 public static void list(int page, String search, String searchFields, String orderBy, String order) {
	        ObjectType type = ObjectType.get(getControllerClass());
	        notFoundIfNull(type);
	        if (page < 1) {
	            page = 1;
	        }
	        List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));
	        Long count = type.count(search, searchFields, (String) request.args.get("where"));
	        Long totalCount = type.count(null, null, (String) request.args.get("where"));
	        try {
	            render(type, objects, count, totalCount, page, orderBy, order);
	        } catch (TemplateNotFoundException e) {
	            render("/Manngers/list.html", type, objects, count, totalCount, page, orderBy, order);
	        }
	    }
	
	   public static void show(String id) throws Exception {
	        ObjectType type = ObjectType.get(getControllerClass());
	        notFoundIfNull(type);
	        Model object = type.findById(id);
	        notFoundIfNull(object);
	        try {
	            render(type, object);
	        } catch (TemplateNotFoundException e) {
	            render("/Manngers/show.html", type, object);
	        }
	    }
	   
	   public static void blank() throws Exception {
	        ObjectType type = ObjectType.get(getControllerClass());
	        notFoundIfNull(type);
	        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
	        constructor.setAccessible(true);
	        Model object = (Model) constructor.newInstance();
	        try {
	            render(type, object);
	        } catch (TemplateNotFoundException e) {
	            render("/Manngers/blank.html", type, object);
	        }
	    }
	   
	   public static void addmanngers(){
		   List<Role> rolelist = Role.findAll();
		   List<Permis> permislist = Permis.findAll();
		   render("/Manngers/addmanngers.html",rolelist,permislist);
	   }
	   
	   public static void savemanngers(String roletype,String uname,String password,String relname,String state,String[] permis ){
		   int states = 0 ;
		   try{
			   System.out.println(roletype+"|"+uname+"|"+password+"|"+relname+"|"+state+"|"+permis);
			   Manager manager = new Manager();
			   manager.rolid = roletype ;
			   manager.mannum = uname ;
			   manager.manpass = Util.md5(password);
			   manager.manname = relname ;
			   manager.manstates = Integer.parseInt(state) ;
			   manager.save();
			   Long mid = manager.id ;
			   for(int i = 0 ;i<permis.length ; i++){
				   PermisAmong peram = new PermisAmong();
				   peram.mgid = mid ;
				   Permis pm = Permis.find("from Permis where percode = ?",permis[i]).first();
				   peram.perid = pm.id ;
				   peram.save();
			   }
			   states = 1 ;
			   render(states);
		   }catch(Exception e){
			   states = 0 ;
			   e.printStackTrace();
			   render(states);
		   }
	   }
}
